package com.Endurancer.manager.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.Endurancer.common.utils.DateUtils;
import com.Endurancer.manager.domain.BlogPost;

import com.Endurancer.manager.domain.dto.CollectDto;
import com.Endurancer.manager.mapper.BlogPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.Endurancer.manager.mapper.CollectionsMapper;
import com.Endurancer.manager.domain.Collections;
import com.Endurancer.manager.service.ICollectionsService;

/**
 * 收藏Service业务层处理
 *
 * @author Endurancer
 * @date 2025-02-04
 */
@Service
public class CollectionsServiceImpl implements ICollectionsService
{
    @Autowired
    private CollectionsMapper collectionsMapper;
    @Autowired
    private BlogPostMapper blogPostMapper;
    /**
     * 查询收藏
     *
     * @param collectionId 收藏主键
     * @return 收藏
     */
    @Override
    public Collections selectCollectionsByCollectionId(Long collectionId)
    {
        return collectionsMapper.selectCollectionsByCollectionId(collectionId);
    }

    /**
     * 查询收藏列表
     *
     * @param collections 收藏
     * @return 收藏
     */
    @Override
    public List<Collections> selectCollectionsList(Collections collections)
    {
        return collectionsMapper.selectCollectionsList(collections);
    }

    /**
     * 新增收藏
     *
     * @param collections 收藏
     * @return 结果
     */
    @Override
    public int insertCollections(Collections collections)
    {
        collections.setCreateTime(DateUtils.getNowDate());
        return collectionsMapper.insertCollections(collections);
    }

    /**
     * 修改收藏
     *
     * @param collections 收藏
     * @return 结果
     */
    @Override
    public int updateCollections(Collections collections)
    {
        collections.setUpdateTime(DateUtils.getNowDate());
        return collectionsMapper.updateCollections(collections);
    }

    /**
     * 批量删除收藏
     *
     * @param collectionIds 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectionsByCollectionIds(Long[] collectionIds)
    {
        return collectionsMapper.deleteCollectionsByCollectionIds(collectionIds);
    }

    /**
     * 删除收藏信息
     *
     * @param collectionId 收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectionsByCollectionId(Long collectionId)
    {
        return collectionsMapper.deleteCollectionsByCollectionId(collectionId);
    }

    @Override
    public Boolean isCollection(CollectDto collectDto) {
        BlogPost blogPost = blogPostMapper.selectBlogPostByPostId(collectDto.getFid());
        Collections dbCollection = collectionsMapper.selectUserLike(collectDto);
        Collections Collect = new Collections();
        BeanUtil.copyProperties(collectDto, Collect);
        if (dbCollection == null) {
            collectionsMapper.insertCollections(Collect);
            blogPost.setCollectionsCount(blogPost.getCollectionsCount() + 1);
            blogPostMapper.updateBlogPost(blogPost);
            return true;
        } else {
            collectionsMapper.deleteCollectionsByCollectionId(dbCollection.getCollectionId());
            blogPost.setCollectionsCount(blogPost.getCollectionsCount() - 1);
            blogPostMapper.updateBlogPost(blogPost);
            return false;
        }
    }
}
